/*
 * Copyright (C) <2009>  <Matthew Dickinson>
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

package com.pulsarwebdevelopment.concordance.plugins;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;

//import javax.swing.JOptionPane;

import com.pulsarwebdevelopment.concordance.Concordance;
import com.pulsarwebdevelopment.concordance.ConcordanceOutputStream;
import com.pulsarwebdevelopment.concordance.Verse;

/**
 * Writes a concordance/subset of a concordance to a file.
 * @author Matthew Dickinson
 * Last updated: 8/15/2009
 */
public class ListGenerator implements Plugin {
	
	//@Override
	public void go(Object[] args) {
		int concIndex = (Integer) args[0];
		File exportFile = (File) args[1];
		int num = Integer.parseInt((String) args[2]);
		String command = (String) args[3];

		BufferedWriter outFile = null;
		try{
			outFile = new BufferedWriter(new FileWriter(exportFile));
		
			Concordance conc = new Concordance(concIndex);
			ConcordanceOutputStream.ListStyle outputStyle = ConcordanceOutputStream.ListStyle.ONE_PER_LINE_TRUNCATED;
			
			//Call the word set method and pass in the output object (appropriate to style) and close the stream
			if(command.equals("Alphabetically")){
				ConcordanceOutputStream<String, Verse> output = 
					new ConcordanceOutputStream<String, Verse>(outFile, outputStyle);
				conc.getWordSetWithVerses(num, output);
				output.close();
			}else{
				ConcordanceOutputStream<Verse, String> output = 
					new ConcordanceOutputStream<Verse, String>(outFile, outputStyle);
				conc.getWordSetReferenced(num, output);
				output.close();
			}
		
		}catch(Exception e){
			//TODO display error message
			//JOptionPane.showMessageDialog(null, e.getMessage());
			//if something went wrong, then close the stream (otherwise it will be closed inthe normal code)
			if(outFile != null){
				try{
					outFile.close();
				}catch(IOException ioe){
				}
			}
		}
		

	}

	//@Override
	public void plug(PluginScreen screen) {
		screen.setTitleText("List Generator", "Generate lists of word pairs, &c. from specified concordance");
		screen.giveConcList("Choose the concordance to use.");
		screen.giveFileBrowser(FileBrowser.FileType.TEXT, "Choose file to output results to.");
		screen.giveTextField("Word set that you want your concordance to include (1 for unique, 2 for pairs). " +
				"Enter a 0 for the whole concordance.");
		screen.giveRadioButtons("Choose how you want the words ordered", "By Reference", "Alphabetically");
	}

}
